DOM XSS in document.write sink using source location.search
Let's insert the following payload in the search field:
test_payload
We can now open Left CLick > Inspect to open the developer tools and search our payload.
We can see that our payload has been inserted in the <img> tag more specifically, it has been appended to the source of the image.
Right above that we can see a <script> tag which includes the script responsible for the DOM manipulation:
function trackSearch(query) {
document.write('<img src="/resources/images/tracker.gif?searchTerms=' + query + '">');
}
var query = (new URLSearchParams(window.location.search)).get('search');
if (query) {
trackSearch(query);
}
- The
trackSearchfunction takes aqueryparameter and writes an image tag to the document, where thesrcattribute includes the search terms. - The
queryvariable is then assigned the value of the 'search' parameter from the URL usingURLSearchParams. - If the 'search' parameter exists in the URL, the
trackSearchfunction is called with the obtained query.
Now that we know how the DOM manipulation works, we can insert our final payload into the application which will generate an alert.
"><svg onload=alert(1)>
We have solved the lab.